﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luncher
{
    public partial class LuncherForm : Form
    {
        private Point mouseOff;         //记录鼠标指针的坐标
        private bool _isLButtonDown;
        private const int USER = 0x0400;
        private IntPtr m_hImc;
        private string[] args;
        private string jsonsetting;
        public LuncherForm(string[] args)
        {
             this.args = args;
            InitializeComponent();
            int init = NativeMethods.av_Init();
            if (init ==0) 
            {
                //this.Width = 1280;
                //this.Height = 720 + 72;

                this.Width = 480;
                this.Height = 640 + 72;

                if (this.args.Length == 3)
                {
                    this.Width = int.Parse(args[1]);
                    this.Height = int.Parse(args[2]) + 72;
                }
                try
                {
                    if (this.args.Length > 0)
                    {
                        jsonsetting = new Setting().GenConfig(args[0]);
                        int ret = NativeMethods.av_Lunch(
                            this.Handle, 
                            panel_main.Location.X, 
                            panel_main.Location.Y,
                            panel_main.Width, 
                            panel_main.Height,
                            jsonsetting);
                    }
                    else
                    {
                        jsonsetting = new Setting().GenConfig("67d60b3f-1069-445b-85ce-bcf49e3d617d");
                        int ret = NativeMethods.av_Lunch(
                            this.Handle, 
                            panel_main.Location.X, 
                            panel_main.Location.Y, 
                            panel_main.Width, 
                            panel_main.Height,
                            jsonsetting);
                    }
                    return;
                }
                catch
                {
                   
                }
            }
            //NativeMethods.FrameBuffer_initialize(panel_main.Width, panel_main.Height);
            //"90de125d-0504-471b-8835-9c1fc37c7eaf
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tableLayoutPanel2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X - tableLayoutPanel2.Location.X, -e.Y - tableLayoutPanel2.Location.Y); //得到变量的值
                _isLButtonDown = true;
            }
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOff = new Point(-e.X, -e.Y); //得到变量的值
                _isLButtonDown = true;            //点击左键按下时标注为true;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isLButtonDown)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOff.X, mouseOff.Y);  //设置移动后的位置
                Location = mouseSet;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (_isLButtonDown)
            {
                _isLButtonDown = false;//释放鼠标后标注为false;
            }
        }

        private void LuncherForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void return_btn_Click(object sender, EventArgs e)
        {
            //var 1 from android source device/uiinstudio/common/xxbMouse.kl   
            NativeMethods.av_KeyEvent(1);
        }

        private void home_btn_Click(object sender, EventArgs e)
        {
            //var 125 from android source device/uiinstudio/common/xxbMouse.kl   
            NativeMethods.av_KeyEvent(125);
        }

        private void home_btn_MouseHover(object sender, EventArgs e)
        {
            home_btn.Image = global::Luncher.Properties.Resources.bar_home_press;
        }

        private void home_btn_MouseLeave(object sender, EventArgs e)
        {
            home_btn.Image = global::Luncher.Properties.Resources.bar_home_normal;
        }

        private void return_btn_MouseHover(object sender, EventArgs e)
        {
            return_btn.Image = global::Luncher.Properties.Resources.bar_back_press;
        }

        private void return_btn_MouseLeave(object sender, EventArgs e)
        {
            return_btn.Image = global::Luncher.Properties.Resources.bar_back_normal;
        }

        private void recent_btn_MouseHover(object sender, EventArgs e)
        {
            recent_btn.Image = global::Luncher.Properties.Resources.bar_recent_press;
        }

        private void recent_btn_MouseLeave(object sender, EventArgs e)
        {
            recent_btn.Image = global::Luncher.Properties.Resources.bar_recent_normal;
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_KEYDOWN = 0x0100;
            const int WM_KEYUP = 0x0101;
            const int WM_IME_CHAR = 0x0286;
            const int Wm_MiscHWHEEL = 0x020E;
            const int WM_IME_SETCONTEXT = 0x0281;
            const int WM_DROPFILES = 0x0233;

            if (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)
            {
                NativeMethods.ImmAssociateContext(this.Handle, m_hImc);
            }
            else if (m.Msg == WM_KEYUP || m.Msg == WM_KEYDOWN || m.Msg == WM_IME_CHAR || m.Msg == Wm_MiscHWHEEL || m.Msg == WM_DROPFILES )
            {
                if (NativeMethods.av_wProc(m.HWnd, m.Msg, m.WParam)) return;
            }
            base.DefWndProc(ref m);
        }

        private void LuncherForm_Load(object sender, EventArgs e)
        {
            m_hImc = NativeMethods.ImmGetContext(this.Handle);
            NativeMethods.DragAcceptFiles(this.Handle, true);
        }
        private void panel_main_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                panel_main.Capture = true;
                NativeMethods.av_MouseEvent(1, e.X, e.Y, 1); ;    //1按下 x y slotindex
                _isLButtonDown = true;
            }
        }

        private void panel_main_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                NativeMethods.av_MouseEvent(0, e.X, e.Y, 1);
                _isLButtonDown = false;
                panel_main.Capture = false;
            }
            //if (NativeMethods.HasCapture(panel_main.Handle)) NativeMethods.ReleaseCapture();
        }

        private void panel_main_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isLButtonDown)
            {//表示此时已经按下鼠标左键;		
                NativeMethods.av_MouseEvent(2, e.X, e.Y, 1);
            }
        }

        private void recent_btn_Click(object sender, EventArgs e)
        {//139 from android source device/uiinstudio/common/xxbMouse.kl   
            NativeMethods.av_KeyEvent(139);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string a = Marshal.PtrToStringAuto(NativeMethods.av_ListVMS());
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {/*
            var format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
            var lockMode = System.Drawing.Imaging.ImageLockMode.WriteOnly;
            var bitmap = new Bitmap(this.panel_main.Width, this.panel_main.Height, format);
            var bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bmpData = bitmap.LockBits(bitmapRect, lockMode, format);
            NativeMethods.av_Screenshot(this.panel_main.Width, this.panel_main.Height, bmpData.Scan0);
            bitmap.UnlockBits(bmpData);

            if (!Directory.Exists(Environment.CurrentDirectory + @"\Screenshot\"))
            {
                Directory.CreateDirectory(Environment.CurrentDirectory + @"\Screenshot\");
            }
            bitmap.Save(Environment.CurrentDirectory + @"\Screenshot\" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss") + ".png", ImageFormat.Png);
            //bitmap.Save(Environment.CurrentDirectory + @"\Screenshot\" + DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss") + ".png", ImageFormat.Png);
          */

            string a = Marshal.PtrToStringAuto(NativeMethods.av_ListVMS());
            string b = a;
        }

        private void panel_main_Paint(object sender, PaintEventArgs e)
        {
            const int WM_PAINT = 0x000F;
            NativeMethods.av_wProc(IntPtr.Zero, WM_PAINT, IntPtr.Zero);
        }

 
    }
}
